import pandas as pd
from Parse_Setfos import Parse_Setfos
from Filter import  Filter, Matching,Replacing
from CE_WAD import CE_WAD,Diff_UV
import os
import colorama
import json
colorama.init(autoreset=True)
TEXT_COLOR_RED = colorama.Fore.RED
TEXT_COLOR_GREEN = colorama.Fore.GREEN
TEXT_COLOR_YELLOW = colorama.Fore.YELLOW
TEXT_COLOR_END = colorama.Fore.RESET


# input_root_path=os.path.join(os.getcwd(),'.\\Data_process\\Raw_data')
# out_root_path = None
# r_ntry=1
# while  os.path.exists(os.path.join(os.getcwd(), '.\\Data_process\\Results', f"Try_{r_ntry}")):
#     r_ntry+=1
# else:
#     os.makedirs(os.path.join(os.getcwd(), '.\\Data_process\\Results', f"Try_{r_ntry}"))
#     out_root_path = os.path.join(os.getcwd(), '.\\Data_process\\Results', f"Try_{r_ntry}")


def fout_method(input_file_name,process_name,out_format):
    input_root_path=os.path.join(os.getcwd(),'.\\Data_process\\Raw_data')
    out_root_path = None
    r_ntry=1
    while  os.path.exists(os.path.join(os.getcwd(), '.\\Data_process\\Results', f"Try_{r_ntry}")):
        r_ntry+=1
    else:
        os.makedirs(os.path.join(os.getcwd(), '.\\Data_process\\Results', f"Try_{r_ntry}"))
        out_root_path = os.path.join(os.getcwd(), '.\\Data_process\\Results', f"Try_{r_ntry}")

    info_list=input_file_name.split(".")[0].split("-")[0].split("_")
    if process_name in ["RGB2W", "MatchedRGB",  "CE_WAD"]:
        fout_prefix = f'{process_name}_' + info_list[-1]
    else:
        fout_prefix = f'{process_name}_'+"_".join(info_list[1:])
    ntry = 0
    while True:
        if ntry == 0:
            fout = fout_prefix + f'.{out_format}'
        else:
            fout = fout_prefix + '-%d.%s' % (ntry,out_format)
        if os.access(os.path.join(out_root_path,fout), os.F_OK):
            ntry += 1
        else:
            break
    return fout

def sort_fout(fout_list_keys):
    list_R = []
    list_G = []
    list_B = []
    inpj="yes"
    for f_name in fout_list_keys:
        # info=f_name.split(".")[0].split("_")
        info=f_name.split(".")[0]
        # if 'R' in info:
        if 'R_' in info:
            list_R.append(f_name)
        if 'G_' in info:
            list_G.append(f_name)
        if 'B_' in info:
            list_B.append(f_name)
    def my_sort(s):
        info=s.split("_")[-1]
        return info
    list_R.sort(key=lambda x:my_sort(x))
    list_B.sort(key=lambda x:my_sort(x))
    list_G.sort(key=lambda x:my_sort(x))
    return list_R,list_G,list_B

def find_coef_joson(inp_list,zf=True):
    coef_config='Parse_Setfos_coef.json'
    print(inp_list)
    if zf:
        if "Ref" in " ".join(inp_list):
            # print(2, inp_list)
            if "R_Ref" in " ".join(inp_list):
                print("*************************************可以匹配上 R_Ref")
                # print(3, inp_list)
                coef_config = 'Parse_Setfos_coef_R.json'
            if "G_Ref" in " ".join(inp_list):
                print("************************************可以匹配上 G_Ref")
                coef_config = 'Parse_Setfos_coef_G.json'
            if "B_Ref" in " ".join(inp_list):
                print("****************************************可以匹配上 B_Ref")
                coef_config = 'Parse_Setfos_coef_B.json'
    # with open(os.path.join('./Parse_Setfos', coef_config), 'r') as f:
        # coef_json = json.load(f)
    # print(coef_json)
    return coef_config

def dataprogress():


    input_root_path=os.path.join(os.getcwd(),'.\\Data_process\\Raw_data')
    out_root_path = None
    r_ntry=1
    while  os.path.exists(os.path.join(os.getcwd(), '.\\Data_process\\Results', f"Try_{r_ntry}")):
        r_ntry+=1
    else:
        os.makedirs(os.path.join(os.getcwd(), '.\\Data_process\\Results', f"Try_{r_ntry}"))
        out_root_path = os.path.join(os.getcwd(), '.\\Data_process\\Results', f"Try_{r_ntry}")

    inps= input('Wheather Doing “周粉” ？, Enter [yes] to DO ; Enter [no] to Not DO : \n').strip().split()
    if inps[0].lower()=="yes":
        P2_bool = True
    else:
        P2_bool=False


    print(TEXT_COLOR_RED + "Doing Step 1 :Parse_Setfos" + TEXT_COLOR_END)
    P1_bool=True
    while True:
        fout1_list = {}
        # try:
        if P1_bool:
            finp_1 = input('Enter file corresponding to "opt2D_R/G/B_ref/5/10.txt ": \n').strip().split()
            
            for inp in finp_1:

                if "[" in inp:
                    inp_list=inp[1:-1].split(",")
                    # print(inp_list)
                else:
                    inp_list=[inp]
                    
                # peng10.li modify at 2024.1.25
                # coef_config=find_coef_joson(inp_list, zf=P2_bool)
                # print(inp_list)
                coef_config=find_coef_joson(inp_list, zf=True)   # 周粉均需要乘Ref_RGB系数
                
                lines_list = []
                for finp_name in inp_list:

                    with open(os.path.join(input_root_path, finp_name)) as f:
                        lines = f.readlines()
                        # lines.append("额外的数据行\t\n")
                        # print(lines)
                        lines_list.append(lines)
                print("******")
                print(inp_list)
                print("******")
                Parse_df, cond_cols = Parse_Setfos.main(lines_list,coef_config,inp_list)


                fout_setfos = fout_method(inp_list[0], 'Setfos', "csv")
                # print("&&&&")
                # print(fout_setfos)
                # print("&&&&")
                Parse_df.to_csv(os.path.join(out_root_path, fout_setfos), index=False)
                print('%d Sweep Conditions are found in %s: %s' % (len(cond_cols), inp, ' '.join(cond_cols)))
                print('Total rows:' + TEXT_COLOR_GREEN + ' %d' % (len(Parse_df)) + TEXT_COLOR_END)
                print('Write output file ' + TEXT_COLOR_GREEN + "%s" % (fout_setfos,) + TEXT_COLOR_END + ' successfully!')
                fout1_list[fout_setfos]=Parse_df.reset_index(drop=True)


        # except Exception as e:
        #     print(TEXT_COLOR_RED + "SetFos Error!!" + TEXT_COLOR_END, f" : {e}",
        #           TEXT_COLOR_RED + " \n Please ReDO ......" + TEXT_COLOR_END)
        else:
            break


    print(TEXT_COLOR_RED+"\nDoing Step 2 : Filter and Matching"+TEXT_COLOR_END)
    print("&&&&")
    print(fout1_list.keys())
    print("&&&&")
    list_R,list_G,list_B=sort_fout(fout1_list.keys())
    # list_B,list_G,list_R=sort_fout(fout1_list.keys())
    print("Files : "+TEXT_COLOR_GREEN+"%s %s %s "%(list_R,list_G,list_B)+TEXT_COLOR_END)
    fout2_list = {}
    print("&&&&")
    print(list_R)
    print("&&&&")
    if P2_bool:
        num=len(list_R)
        find_ref=False
        idx=num-1
        while idx>0:
            if "Ref" in list_R[idx]:
                find_ref=True
            if find_ref:
                for list_color in [list_R,list_G,list_B]:
                    while True:
                        print(TEXT_COLOR_YELLOW + f"Doing Step 2-1 :Filter {list_color[idx]}" + TEXT_COLOR_END)
                        try:
                            Filtered=Filter.main(fout1_list[list_color[idx]])
                        except Exception as e:

                            print(TEXT_COLOR_RED + "Filter Error!!" + TEXT_COLOR_END, f" : {e}",
                                TEXT_COLOR_RED + " \n Please ReDO Filteing......" + TEXT_COLOR_END)
                        else:
                            break
                    fout_ref = fout_method(list_color[idx], 'Filtered', "csv")
                    Filtered.to_csv(os.path.join(out_root_path, fout_ref), index=False)
                    print('Total rows:' + TEXT_COLOR_GREEN + ' %d' % (len(Filtered)) + TEXT_COLOR_END)
                    print('Write output file ' + TEXT_COLOR_GREEN + "%s" % (fout_ref,) + TEXT_COLOR_END + ' successfully!')
                    fout2_list[fout_ref]=Filtered

                    n=num-1
                    index=idx
                    while n>0:
                        index=(index+1)%num
                        print(TEXT_COLOR_YELLOW+f"Doing Step 2-2 :Matching {list_color[index]}"+TEXT_COLOR_END)
                        coef=list_color[index].split(".")[0].split("-")[0].split("_")[-1]
                        # coef = list_color[index].split("Ref")[0].split("_")[-1]
                        print(f"coef={coef}")
                        if coef.isdigit():
                            coef = coef
                        else:
                            print("Can not recognize coef from File_name of %s" % list_color[index])
                            coef = input("Please set the coef of Sion.B.d for Ref :").strip()
                        while True:
                            try:
                                matched_df= Matching.main(fout2_list[fout_ref],fout1_list[list_color[index]],coef)
                            except Exception as e:
                                print(TEXT_COLOR_RED + "Matching Error!!" + TEXT_COLOR_END, f" : {e}",
                                    TEXT_COLOR_RED + " \n Please ReDO Filteing and Matching......" + TEXT_COLOR_END)
                            else:
                                break

                        fout_match = fout_method(list_color[index], "Matched", "csv")
                        matched_df.to_csv(os.path.join(out_root_path, fout_match), index=False)
                        print('Total rows:' + TEXT_COLOR_GREEN + ' %d' % (len(matched_df)) + TEXT_COLOR_END)
                        print('Write output file ' + TEXT_COLOR_GREEN + "%s" % (fout_match,) + TEXT_COLOR_END + ' successfully!')
                        n-=1
                        fout2_list[fout_match]=matched_df
                break
            idx-=1
        else:
            print("DO Not Find Ref File!")
    else:
        filter_bool=input("Enter [yes] to Do Filter,Else Jump Filter: \n")
        if filter_bool.lower()=="yes":
            print(f"current files that you can choose to be filter :{fout1_list.keys()}")
            finp = input('Please Choose the file you want to Filter From List Above :\n').strip().split()
            exclude_list=list(set(fout1_list.keys())-set(finp))
            for finp_name in finp:
                print(TEXT_COLOR_YELLOW + f"Filtering {finp_name}" + TEXT_COLOR_END)
                Filtered=Filter.main(fout1_list[finp_name])
                fout_filter=fout_method(finp_name,"Filtered","csv")
                Filtered.to_csv(os.path.join(out_root_path, fout_filter), index=False)
                print('Total rows:' + TEXT_COLOR_GREEN + ' %d' % (len(Filtered)) + TEXT_COLOR_END)
                print('Write output file ' + TEXT_COLOR_GREEN + "%s" % (fout_filter,) + TEXT_COLOR_END + ' successfully!')
                fout2_list[fout_filter]=Filtered
            for f_name in exclude_list:
                fout2_list[f_name ]=fout1_list[f_name]
        else:
            fout2_list=fout1_list

    PR_bool=input(TEXT_COLOR_YELLOW+"\nEnter [Yes] to Do Raplacing; Else Jump Replacing\n"+TEXT_COLOR_END).strip().lower()=="yes"
    PR_list = {}
    print("******")
    print(fout2_list)#[]
    print("******")
    if PR_bool:
        print(TEXT_COLOR_YELLOW + "Doing Step 2-3 : Replacing Some Values " + TEXT_COLOR_END)
        list_R, list_G, list_B = sort_fout(fout2_list.keys())
        n_file = len(list_R)
        for color in [list_R,list_G,list_B]:
            #Find Ref
            if P2_bool:
                inp_key = []
                inp_list = []
                idx = 0
                num=n_file
                find_ref=False
                while num>0:
                    if "Ref" in color[idx]:
                        find_ref = True
                    if find_ref:
                        inp_key.append(color[idx])
                        inp_list.append(fout2_list[color[idx]])
                        num -= 1
                    idx = (idx + 1) % n_file
            else:
                inp_key=color
                inp_list=[fout2_list[key] for key in color]
            #Repalcing
            print("Replacing File : "+TEXT_COLOR_RED+"%s" % (" ".join(inp_key))+TEXT_COLOR_END)
            while True:
                print("******")
                print(inp_list)#[]
                print("******")
                file_list=inp_list
                # try:
                new_file_list=Replacing.main(P2_bool,file_list)
                # except Exception as e:
                #     print(TEXT_COLOR_RED + "Replacing Error!!" + TEXT_COLOR_END, f" : {e}",
                #           TEXT_COLOR_RED + " \n Please ReDO Replacing......" + TEXT_COLOR_END)
                # else:
                #     break
                for key in inp_key:
                    replaced_fout=fout_method(key,"Replaced","csv")
                    new_file_list[inp_key.index(key)].to_csv(os.path.join(out_root_path, replaced_fout), index=False)
                    print('Write output file ' + TEXT_COLOR_GREEN + "%s" % (replaced_fout,) + TEXT_COLOR_END + ' successfully!')
                    PR_list[replaced_fout] = new_file_list[inp_key.index(key)]
    else:
        PR_list=fout2_list


    print(TEXT_COLOR_RED + "\nDoing Step 3 :Caculate CE_WAD " + TEXT_COLOR_END)
    list_R,list_G,list_B=sort_fout(PR_list.keys())
    n_file=len(list_R)
    print("Files : "+TEXT_COLOR_GREEN+"%s %s %s "%(list_R,list_G,list_B)+TEXT_COLOR_END)
    P3_bool=True
    P3_list={}
    if P3_bool :
        index=0
        num=n_file
        find_ref=False
        config = {}
        while num>0:
            if "Ref"  in list_R[index] :
                find_ref=True
            if find_ref and config and P2_bool:
                zf = True
            else:
                zf=False
            if find_ref or (not P2_bool):
                while True:
                    print( "list To Do CE_WAD : " + TEXT_COLOR_RED + f"{[list_R[index], list_G[index], list_B[index]]}:" + TEXT_COLOR_END)
                    try:
                        rgb2w_df, Matched_rgb, ce_wad_df ,config= CE_WAD.main(PR_list[list_R[index]].reset_index(drop=True), PR_list[list_G[index]].reset_index(drop=True),PR_list[ list_B[index]].reset_index(drop=True),config=config ,zf=zf)
                    except Exception as e:
                        print(TEXT_COLOR_RED + "Canculate Error!!" + TEXT_COLOR_END, f" : {e}",
                            TEXT_COLOR_RED + " \n Please ReDO Caculating......" + TEXT_COLOR_END)
                    else:
                        break
                fout_rgb, fout_mat, fout_ce = fout_method(list_R[index], "RGB2W", "csv"), fout_method(list_R[index], "MatchedRGB", "csv"), fout_method(list_R[index], "CE_WAD", "csv")
                if find_ref and not rgb2w_df.empty:
                    rgb2w_df.to_csv(os.path.join(out_root_path, fout_rgb), index=False)
                    config["ref"]=rgb2w_df
                else:
                    fout_rgb=''
                Matched_rgb.to_csv(os.path.join(out_root_path, fout_mat), index=False)
                ce_wad_df.to_csv(os.path.join(out_root_path, fout_ce), index=False)
                print('Write output file' + TEXT_COLOR_GREEN + " %s,%s,%s " % ( fout_mat, fout_rgb, fout_ce) + TEXT_COLOR_END + ' successfully!')
                P3_list[fout_ce]=ce_wad_df
                num-=1
            index=(index+1)%n_file

    if P2_bool:
        print(TEXT_COLOR_RED + "\nDoing Step 4 : Canculating Diff_U/V" + TEXT_COLOR_END)
        print("Files : "+TEXT_COLOR_GREEN+"%s"%(P3_list.keys(),)+TEXT_COLOR_END)
    else:
        exit()
    P4_bool=P2_bool
    while True:
        try:
            if P4_bool:
                ref_list=[]
                per_list=[]
                for file_name in P3_list.keys():
                    if "Ref" in file_name:
                        ref_list.append(file_name)
                    else:
                        per_list.append(file_name)
                ref=ref_list[0]
                cancu_ref=P3_list[ref]
                ref_columns_list=cancu_ref.columns.to_list()
                out_df=cancu_ref
                for per_file in per_list:
                    suffix = per_file.split(".")[0].split("-")[0].split("_")[-1]
                    df=P3_list[per_file]
                    out_df[f"diff_u_{suffix}"],out_df[f"diff_v_{suffix}"]=df["U"] - cancu_ref["U_0"], df["V"] - cancu_ref["V_0"]
                fout_prefix="Diff_UV_ALL"
                ntry=0
                while True:
                    if ntry==0:
                        fout=fout_prefix+".csv"
                    else:
                        fout=fout_prefix+"-%s.csv"%(ntry,)
                    if os.access(os.path.join(out_root_path,fout), os.F_OK):
                        ntry += 1
                    else:
                        break
                out_df.to_csv(os.path.join(out_root_path,fout),index=False)
                print('Write output file ',TEXT_COLOR_GREEN+"%s"% (fout,)+TEXT_COLOR_END,' successfully!' )
                print('Result Root Path：', TEXT_COLOR_GREEN + "Results\Try_%s" % (r_ntry,) + TEXT_COLOR_END)
        except Exception as e:
            print(TEXT_COLOR_RED+"Error!!"+TEXT_COLOR_END,f" : {e}",
                TEXT_COLOR_RED+" \n Please ReDO ......"+TEXT_COLOR_END)
        else:
            break



# opt2D_B_5.txt opt2D_B_10.txt opt2D_B_Ref.txt opt2D_G_5.txt opt2D_G_10.txt opt2D_G_Ref.txt opt2D_R_5.txt opt2D_R_10.txt opt2D_R_Ref.txt

#Setfos_R_Ref.csv Setfos_G_5.csv Setfos_B_10.csv

# CIEy_0 0.2 0.323 CIEy_0 0.2 0.31353
# CIEy_0 0.6 0.751
# CIEy_0 0.04 0.0453

# [opt2D2_R_Ref.txt,opt2D_R_Ref.txt] opt2D_G_Ref.txt opt2D_B_Ref.txt

# CIEx_0 0.682 0.6835  #CIEx_0 0.235 0.243   # CIEy_0 0.045 0.048
# CIEx_0 0.684 0.6855  #CIEx_0 0.245 0.253   # CIEy_0 0.0485 0.051